import Vue from 'vue'
import Vuex from 'vuex'
import finPersonalAmountArchives from "@/components/archivesmange/FinPersonalAmountArchives";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isCollapsed: false,
    oldPath: window.localStorage.getItem("oldPath"),
    token: window.localStorage.getItem("token"),
    userInfo: window.localStorage.getItem("userInfo")?JSON.parse(window.localStorage.getItem("userInfo")):null,
    // 缓存组件页面
    catch_components: [],
    // 当前选中的菜单 - 默认选择首页
    activePath:window.localStorage.getItem("activePath"),
    // 菜单项 - 默认包含首页
    tabList: window.localStorage.getItem("tabList")?JSON.parse(window.localStorage.getItem("tabList")):null,
    // tabList: [],
    selectedData:{},
    functions: window.localStorage.getItem("functions")?JSON.parse(window.localStorage.getItem("functions")):null,
    menus: window.localStorage.getItem("menus")?JSON.parse(window.localStorage.getItem("menus")):null,
    productLine:window.localStorage.getItem("productLine")?JSON.parse(window.localStorage.getItem("productLine")):null,
    productDate:[],
    firstName:window.localStorage.getItem("firstName"),
    secondName:window.localStorage.getItem("secondName"),
    breadData:window.localStorage.getItem("bread")?JSON.parse(window.localStorage.getItem("bread")):null,
    allBreadData:window.localStorage.getItem("allBreadData")?JSON.parse(window.localStorage.getItem("allBreadData")):null,
    userList:window.localStorage.getItem("userList")?JSON.parse(window.localStorage.getItem("userList")):null,
    socialMedicalActiveName:window.localStorage.getItem("socialMedicalActiveName"),
    dataDetailActiveName:window.localStorage.getItem("dataDetailActiveName"),
    familyAllowanceActiveName:window.localStorage.getItem("familyAllowanceActiveName"),
    personalAmountArchivesActiveName:window.localStorage.getItem("personalAmountArchivesActiveName"),
    childAllowanceActiveName:window.localStorage.getItem("childAllowanceActiveName"),
    filialPietyAwardActiveName:window.localStorage.getItem("filialPietyAwardActiveName"),
    introduceAwardActiveName:window.localStorage.getItem("introduceAwardActiveName"),
    baseAmountArchivesActiveName:window.localStorage.getItem("baseAmountArchivesActiveName"),
    standardWagesActiveName:window.localStorage.getItem("standardWagesActiveName"),
    updatePasswordBoxShow:window.localStorage.getItem("updatePasswordBoxShow")
  },
  getters: {},
  mutations:{
    setUpdatePasswordBoxShow(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("updatePasswordBoxShow");
        return;
      }
      state.updatePasswordBoxShow=value
      window.localStorage.setItem("updatePasswordBoxShow",value)
    },
    removeUpdatePasswordBoxShow(state){
      window.localStorage.removeItem("updatePasswordBoxShow");
    },
    setStandardWagesActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("standardWagesActiveName");
        return;
      }
      state.standardWagesActiveName=value
      window.localStorage.setItem("standardWagesActiveName",value)
    },
    setBaseAmountArchivesActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("baseAmountArchivesActiveName");
        return;
      }
      state.baseAmountArchivesActiveName=value
      window.localStorage.setItem("baseAmountArchivesActiveName",value)
    },
    setIntroduceAwardActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("introduceAwardActiveName");
        return;
      }
      state.introduceAwardActiveName=value
      window.localStorage.setItem("introduceAwardActiveName",value)
    },
    setFilialPietyAwardActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("filialPietyAwardActiveName");
        return;
      }
      state.filialPietyAwardActiveName=value
      window.localStorage.setItem("filialPietyAwardActiveName",value)
    },
    setChildAllowanceActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("childAllowanceActiveName");
        return;
      }
      state.childAllowanceActiveName=value
      window.localStorage.setItem("childAllowanceActiveName",value)
    },
    setPersonalAmountArchivesActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("personalAmountArchivesActiveName");
        return;
      }
      state.personalAmountArchivesActiveName=value
      window.localStorage.setItem("personalAmountArchivesActiveName",value)
    },
    setFamilyAllowanceActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("familyAllowanceActiveName");
        return;
      }
      state.familyAllowanceActiveName=value
      window.localStorage.setItem("familyAllowanceActiveName",value)
    },
    setDataDetailActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("dataDetailActiveName");
        return;
      }
      state.dataDetailActiveName=value
      window.localStorage.setItem("dataDetailActiveName",value)
    },
    setSocialMedicalActiveName(state,value){
      if(!value || value === 'undefined'){
        window.localStorage.removeItem("socialMedicalActiveName");
        return;
      }
      state.socialMedicalActiveName=value
      window.localStorage.setItem("socialMedicalActiveName",value)
    },
    setUserList(state,userList){
      if(!userList || userList === 'undefined'){
        window.localStorage.removeItem("userList");
        return;
      }
      state.userList=userList
      window.localStorage.setItem("userList",JSON.stringify(userList))
    },
    setTabList(state,tabList){
      if(!tabList || tabList === 'undefined'){
        window.localStorage.removeItem("tabList");
        return;
      }
      window.localStorage.setItem("tabList",JSON.stringify(tabList))
    },
    setAllBreadData(state,allBreadData){
      if(!allBreadData || allBreadData === 'undefined'){
        window.localStorage.removeItem("allBreadData");
        return;
      }
      state.allBreadData=allBreadData
      window.localStorage.setItem("allBreadData",JSON.stringify(allBreadData))

    },
    setBread(state,bread){
      if(!bread || bread === 'undefined'){
        window.localStorage.removeItem("bread");
        return;
      }
      state.breadData=bread
      window.localStorage.setItem("bread",JSON.stringify(bread))
    },
    setFunction(state,functions){
      if(!functions || functions === 'undefined'){
        window.localStorage.removeItem("functions");
        return;
      }
      state.functions=functions
      window.localStorage.setItem("functions",JSON.stringify(functions))
    },
    setMenus(state,menus){
      if(!menus || menus === 'undefined'){
        window.localStorage.removeItem("menus");
        return;
      }
      state.menus=menus
      window.localStorage.setItem("menus",JSON.stringify(menus))
    },

    setFirstName(state,first){
      state.firstName=first
      window.localStorage.setItem("firstName",first)
    },
    setSecondName(state,second){
      state.secondName=second
      window.localStorage.setItem("secondName",second)
    },
    setCollapsed(state, isCollapsed) {
      state.isCollapsed = isCollapsed
      window.localStorage.setItem("isCollapsed", isCollapsed)
    },
    setOldPath(state, oldPath) {
      state.oldPath = oldPath
      window.localStorage.setItem("oldPath", oldPath)
    },
    setToken(state, token) {
      state.token = token
      window.localStorage.setItem("token", token)
    },
    setUserInfo(state, userInfo) {
      if(!userInfo || userInfo === 'undefined'){
        window.localStorage.removeItem("userInfo");
        return;
      }
      state.userInfo = userInfo
      window.localStorage.setItem("userInfo", JSON.stringify(userInfo))
    },
    // 跳转页面执行
    selectMenu(state, submenu) {
      // 首页 home
      if (submenu.name === 'home') {
        submenu.name = 'home'
        submenu.path = '/admin/home'
        submenu.class = "el-icon-home"

      }
      // 当前选中菜单
      var activePath = submenu.name
      // 历史已选中菜单列表
      var oldTabList = state.tabList
      // 将菜单信息添加到tablist - 添加时判断是否已有该标签
      var result = oldTabList.some(item => {
        if (item.name === activePath) {
          return true
        }
      })
      // 如果不包含该对象，则添加
      if (!result) {
        oldTabList.push({
          path: submenu.path,
          name: submenu.name,
          label: submenu.label,
          class: submenu.class
        })
      }
      // 重新赋值
      state.activePath = activePath
      window.localStorage.setItem("activePath",activePath)
      state.tabList = oldTabList
    },
    // 添加keepalive缓存
    addKeepAliveCache(state, val) {
      // 如果是首页不缓存
      if (val === 'home') {
        return
      }
      // 添加时判断，如果该组件已存在，则不添加
      if (state.catch_components.indexOf(val) === -1) {
        // 不存在，缓存页面
        state.catch_components.push(val)
      }
    },
    // 删除keepalive缓存
    removeKeepAliveCache(state, val) {
      let cache = state.catch_components
      for (let i = 0; i < cache.length; i++) {
        if (cache[i] === val) {
          cache.splice(i, 1);
        }
      }
      state.catch_components = cache
    },
    //关闭菜单
    closeTab(state, val) {
      // 重新赋值
      state.activePath = val.activePath
      state.tabList = val.tabList
    },
    // 点击标签选择菜单
    changeMenu(state, val) {
      state.activePath = val
    },
  },
  actions: {},
  modules: {}
})
